<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/errors.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../high_level_components.html" title="Chapter 2. High Level Components">
<link rel="prev" href="boost_python_enum_hpp.html" title="boost/python/enum.hpp">
<link rel="next" href="boost_python_exception_translato.html" title="boost/python/exception_translator.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_enum_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_exception_translato.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="high_level_components.boost_python_errors_hpp"></a><a class="link" href="boost_python_errors_hpp.html" title="boost/python/errors.hpp">boost/python/errors.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.class_error_already_set">Class
        <code class="computeroutput"><span class="identifier">error_already_set</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.functions">Functions</a></span></dt>
<dt><span class="section"><a href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_errors_hpp.introduction"></a><a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          &lt;boost/python/errors.hpp&gt; provides types and functions for managing
          and translating between Python and C++ exceptions. This is relatively low-level
          functionality that is mostly used internally by Boost.Python. Users should
          seldom need it.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_errors_hpp.class_error_already_set"></a><a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.class_error_already_set" title="Class error_already_set">Class
        <code class="computeroutput"><span class="identifier">error_already_set</span></code></a>
</h3></div></div></div>
<p>
          error_already_set is an exception type which can be thrown to indicate
          that a Python error has occurred. If thrown, the precondition is that
          <a href="http://www.python.org/doc/2.2/api/exceptionHandling.html#l2h-71" target="_top">PyErr_Occurred()</a>
          returns a value convertible to <code class="computeroutput"><span class="keyword">true</span></code>.
          Portable code shouldn't throw this exception type directly, but should
          instead use <a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.functions" title="Functions">throw_error_already_set()</a>,
          below.
        </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
    <span class="keyword">class</span> <span class="identifier">error_already_set</span> <span class="special">{};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_errors_hpp.functions"></a><a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.functions" title="Functions">Functions</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">handle_exception</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">f</span><span class="special">)</span> <span class="keyword">throw</span><span class="special">();</span>
<span class="keyword">void</span> <span class="identifier">handle_exception</span><span class="special">()</span> <span class="keyword">throw</span><span class="special">();</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                The first form requires that the expression function0&lt;void&gt;(f)
                is valid. The second form requires that a C++ exception is currently
                being handled (see section 15.1 in the C++ standard).
              </p></dd>
<dt><span class="term">Effects</span></dt>
<dd><p>
                The first form calls f() inside a try block which first attempts
                to use all registered <a class="link" href="boost_python_exception_translato.html" title="boost/python/exception_translator.hpp">exception
                translators</a>. If none of those translates the exception, the
                catch clauses then set an appropriate Python exception for the C++
                exception caught, returning true if an exception was thrown, false
                otherwise. The second form passes a function which rethrows the exception
                currently being handled to the first form.
              </p></dd>
<dt><span class="term">Postconditions</span></dt>
<dd><p>
                No exception is being handled
              </p></dd>
<dt><span class="term">Throws</span></dt>
<dd><p>
                nothing
              </p></dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                At inter-language boundaries it is important to ensure that no C++
                exceptions escape, since the calling language usually doesn't have
                the equipment necessary to properly unwind the stack. Use handle_exception
                to manage exception translation whenever your C++ code is called
                directly from the Python API. This is done for you automatically
                by the usual function wrapping facilities: <a class="link" href="../function_invocation_and_creation/boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.functions" title="Functions">make_function()</a>,
                <a class="link" href="../function_invocation_and_creation/boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.functions" title="Functions">make_constructor()</a>,
                <a class="link" href="boost_python_def_hpp.html#high_level_components.boost_python_def_hpp.functions" title="Functions">def()</a>
                and <a class="link" href="../high_level_components.html#high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel.class_template_class_modifier_fu" title="Class template class_ modifier functions">class_::def()</a>.
                The second form can be more convenient to use (see the example below),
                but various compilers have problems when exceptions are rethrown
                from within an enclosing try block.
              </p></dd>
</dl>
</div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">T</span><span class="special">*</span> <span class="identifier">expect_non_null</span><span class="special">(</span><span class="identifier">T</span><span class="special">*</span> <span class="identifier">x</span><span class="special">);</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                x
              </p></dd>
<dt><span class="term">Throws</span></dt>
<dd><p>
                error_already_set() iff x == 0.
              </p></dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                Simplifies error-handling when calling functions in the Python/C
                API which return 0 on error.
              </p></dd>
</dl>
</div>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">throw_error_already_set</span><span class="special">();</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
                throw error_already_set();
              </p></dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                Simplifies error-handling when calling functions in the Python/C
                API which return 0 on error.
              </p></dd>
</dl>
</div>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">throw_error_already_set</span><span class="special">();</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
                throw error_already_set();
              </p></dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                Many platforms and compilers are not able to consistently catch exceptions
                thrown across shared library boundaries. Using this function from
                the Boost.Python library ensures that the appropriate catch block
                in handle_exception() can catch the exception.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_errors_hpp.example"></a><a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.example" title="Example">Example</a>
</h3></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">errors</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">object</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">handle</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// Returns a std::string which has the same value as obj's "__name__"</span>
<span class="comment">// attribute.</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">get_name</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span> <span class="identifier">obj</span><span class="special">)</span>
<span class="special">{</span>
   <span class="comment">// throws if there's no __name__ attribute</span>
   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">expect_non_null</span><span class="special">(</span>
      <span class="identifier">PyObject_GetAttrString</span><span class="special">(</span><span class="identifier">obj</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">(),</span> <span class="string">"__name__"</span><span class="special">));</span>

   <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">s</span> <span class="special">=</span> <span class="identifier">PyString_AsString</span><span class="special">(</span><span class="identifier">p</span><span class="special">);</span>
   <span class="keyword">if</span> <span class="special">(</span><span class="identifier">s</span> <span class="special">!=</span> <span class="number">0</span><span class="special">)</span>
        <span class="identifier">Py_DECREF</span><span class="special">(</span><span class="identifier">p</span><span class="special">);</span>

   <span class="comment">// throws if it's not a Python string</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">result</span><span class="special">(</span>
      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">expect_non_null</span><span class="special">(</span>
         <span class="identifier">PyString_AsString</span><span class="special">(</span><span class="identifier">p</span><span class="special">)));</span>

   <span class="identifier">Py_DECREF</span><span class="special">(</span><span class="identifier">p</span><span class="special">);</span> <span class="comment">// Done with p</span>

   <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span>
<span class="special">}</span>

<span class="comment">//</span>
<span class="comment">// Demonstrate form 1 of handle_exception</span>
<span class="comment">//</span>

<span class="comment">// Place into result a Python Int object whose value is 1 if a and b have</span>
<span class="comment">// identical "__name__" attributes, 0 otherwise.</span>
<span class="keyword">void</span> <span class="identifier">same_name_impl</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*&amp;</span> <span class="identifier">result</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span> <span class="identifier">b</span><span class="special">)</span>
<span class="special">{</span>
   <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">PyInt_FromLong</span><span class="special">(</span>
      <span class="identifier">get_name</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">get_name</span><span class="special">(</span><span class="identifier">a2</span><span class="special">));</span>
<span class="special">}</span>

<span class="identifier">object</span> <span class="identifier">borrowed_object</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">p</span><span class="special">)</span>
<span class="special">{</span>
   <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span><span class="special">(</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">handle</span><span class="special">&lt;&gt;(</span>
             <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">borrowed</span><span class="special">(</span><span class="identifier">a1</span><span class="special">)));</span>
<span class="special">}</span>

<span class="comment">// This is an example Python 'C' API interface function</span>
<span class="keyword">extern</span> <span class="string">"C"</span> <span class="identifier">PyObject</span><span class="special">*</span>
<span class="identifier">same_name</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">args</span><span class="special">,</span> <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">keywords</span><span class="special">)</span>
<span class="special">{</span>
   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">a1</span><span class="special">;</span>
   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">a2</span><span class="special">;</span>
   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">result</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>

   <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">PyArg_ParseTuple</span><span class="special">(</span><span class="identifier">args</span><span class="special">,</span> <span class="keyword">const_cast</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">*&gt;(</span><span class="string">"OO"</span><span class="special">),</span> <span class="special">&amp;</span><span class="identifier">a1</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">a2</span><span class="special">))</span>
      <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>

   <span class="comment">// Use boost::bind to make an object compatible with</span>
   <span class="comment">// boost::Function0&lt;void&gt;</span>
   <span class="keyword">if</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">handle_exception</span><span class="special">(</span>
         <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;(</span><span class="identifier">same_name_impl</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">result</span><span class="special">),</span> <span class="identifier">borrowed_object</span><span class="special">(</span><span class="identifier">a1</span><span class="special">),</span> <span class="identifier">borrowed_object</span><span class="special">(</span><span class="identifier">a2</span><span class="special">))))</span>
   <span class="special">{</span>
      <span class="comment">// an exception was thrown; the Python error was set by</span>
      <span class="comment">// handle_exception()</span>
      <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
   <span class="special">}</span>

   <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span>
<span class="special">}</span>

<span class="comment">//</span>
<span class="comment">// Demonstrate form 2 of handle_exception. Not well-supported by all</span>
<span class="comment">// compilers.</span>
<span class="comment">//</span>
<span class="keyword">extern</span> <span class="string">"C"</span> <span class="identifier">PyObject</span><span class="special">*</span>
<span class="identifier">same_name2</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">args</span><span class="special">,</span> <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">keywords</span><span class="special">)</span>
<span class="special">{</span>
   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">a1</span><span class="special">;</span>
   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">a2</span><span class="special">;</span>
   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">result</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>

   <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">PyArg_ParseTuple</span><span class="special">(</span><span class="identifier">args</span><span class="special">,</span> <span class="keyword">const_cast</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">*&gt;(</span><span class="string">"OO"</span><span class="special">),</span> <span class="special">&amp;</span><span class="identifier">a1</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">a2</span><span class="special">))</span>
      <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>

   <span class="keyword">try</span> <span class="special">{</span>
      <span class="keyword">return</span> <span class="identifier">PyInt_FromLong</span><span class="special">(</span>
         <span class="identifier">get_name</span><span class="special">(</span><span class="identifier">borrowed_object</span><span class="special">(</span><span class="identifier">a1</span><span class="special">))</span> <span class="special">==</span> <span class="identifier">get_name</span><span class="special">(</span><span class="identifier">borrowed_object</span><span class="special">(</span><span class="identifier">a2</span><span class="special">)));</span>
   <span class="special">}</span>
   <span class="keyword">catch</span><span class="special">(...)</span>
   <span class="special">{</span>
      <span class="comment">// If an exception was thrown, translate it to Python</span>
      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">handle_exception</span><span class="special">();</span>
      <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
   <span class="special">}</span>
<span class="special">}</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_enum_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_exception_translato.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
